package top.jacktgq.web.struts2;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import top.jacktgq.dao.StudentDao;
import top.jacktgq.pojo.PageBean;
import top.jacktgq.pojo.Student;
import top.jacktgq.utils.MapperUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author CandyWall
 * @Date 2021/6/20--12:34
 * @Description
 */
public class GetStudentsByConditionAction extends ActionSupport {
    private Integer currentCount;
    private Integer currentPage;
    private String name;
    private String sex;

    public void setCurrentCount(Integer currentCount) {
        this.currentCount = currentCount;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void getStudentsByCondition() {
        HttpServletResponse response = ServletActionContext.getResponse();
        name = new String(name.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);

        StudentDao studentDao = new StudentDao();

        //调用业务层分页查询所有的装备信息
        PageBean<Student> pageBean = null;
        String msg = "查询成功";
        try {
            pageBean = studentDao.getStudentsByCondition(currentPage, currentCount, name, sex);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            msg = "查询失败";
        }
        Map<String, Object> pageBeanMap = new HashMap<String, Object>();
        pageBeanMap.put("status", 200);
        pageBeanMap.put("hint", msg);
        pageBeanMap.put("total", pageBean.getTotalCount());
        pageBeanMap.put("rows", pageBean.getList());
        System.out.println("GetStudentsByConditionServlet方法返回值========");
        System.out.println(pageBeanMap);
        System.out.println("GetStudentsByConditionServlet方法返回值========");

        response.setCharacterEncoding("utf-8");
        response.setContentType("charset=utf-8;json/html");
        try {
            response.getWriter().write(MapperUtils.obj2json(pageBeanMap));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
